package net.itbaiyang.pattern.email.bridge.bridgeMajor;

import lombok.extern.slf4j.Slf4j;
import net.itbaiyang.pattern.email.bridge.mailType.MailStencil;
import net.itbaiyang.pattern.util.JsonData;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

import java.util.Map;

@Slf4j
public class DefaultMailSender extends MailSender{

    @Override
    public JsonData send(Map<String, Object> mailInfo,JavaMailSender mailSender) {
        try {
//            MailStencil template = (MailStencil)mailInfo.get("mailStencil1");
            MimeMessageHelper helper = (MimeMessageHelper)mailInfo.get("helper");
//            template.apply(helper);
            mailSender.send(helper.getMimeMessage());
            log.info("邮件已经发送。");
        } catch (Exception e) {
            log.error("发送邮件时发生异常!", e);
            return JsonData.buildError("消息发送失败");
        }
        return JsonData.buildCodeAndMsg(1,"消息发送成功");
    }

}
